#include "dialog.h"
#include <QHBoxLayout>
#include <QDebug>
#include <QOpenGLWidget>
#include <GL/glu.h>
#include <QOpenGLContext>

class OGLWidget : public QOpenGLWidget
{
public:
    explicit OGLWidget(QWidget* parent = Q_NULLPTR) : QOpenGLWidget(parent)
    {
    }

    virtual ~OGLWidget()
    {
    }

protected:
    void paintGL() override {}
    void initializeGL() override {}
    void resizeGL(int w, int h) override {}

public:
    void printContextInformation()
    {
        qDebug() << "---------------------------------------------------------------";

        const GLubyte* vendor = glGetString(GL_VENDOR);
        const GLubyte* render = glGetString(GL_RENDERER);
        QString openGLType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
        const GLubyte* openGLVersion = glGetString(GL_VERSION);

        GLint major, minor;
        glGetIntegerv(GL_MAJOR_VERSION, &major);
        glGetIntegerv(GL_MINOR_VERSION, &minor);

        // 获取 Profile 信息
        QString openGLProfile;

#define CASE(c) case QSurfaceFormat::c: openGLProfile = #c; break
        switch (format().profile())
        {
                CASE(NoProfile);
                CASE(CoreProfile);
                CASE(CompatibilityProfile);
        }
#undef CASE

        const GLubyte* glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);
        const GLubyte* gluVersion = gluGetString(GLU_VERSION);
        const GLubyte* openGLExtensions = glGetString(GL_EXTENSIONS);
        const GLubyte* gluExtensions = gluGetString(GLU_EXTENSIONS);

        qDebug() << "OpenGL vendor: " << (const char*)vendor;
        qDebug() << "render: " << (const char*)render;
        qDebug() << "OpenGL type: " << openGLType;
        qDebug() << "OpenGL version: " << (const char*)openGLVersion << "(" << major << minor << ")";
        qDebug() << "OpenGL profile: " << openGLProfile;
        qDebug() << "GLSL version: " << (const char*)glslVersion;
        qDebug() << "GLU version: " << (const char*)gluVersion;
        qDebug() << "OpenGL extensions: " << (const char*)openGLExtensions;
        qDebug() << "GLU extensions: " << (const char*)gluExtensions;
    }
};

Dialog::Dialog(QWidget* parent)
    : QDialog(parent)
{
    w = new OGLWidget(this);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(w);

    setLayout(layout);

    setMinimumSize(400, 300);
}

Dialog::~Dialog()
{
    delete w;
}

void Dialog::keyPressEvent(QKeyEvent*)
{
    w->printContextInformation();
}
